package cn.com.thingo.shyintelligent.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.RemoterInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.base.BaseActivity;
import cn.com.thingo.shyintelligent.base.contract.AirConditionContract;
import cn.com.thingo.shyintelligent.presenter.AirConditionPresenter;
import cn.com.thingo.shyintelligent.ui.adapter.AirConditionListAdapter;
import cn.com.thingo.shyintelligent.ui.adapter.decoration.RecycleViewDivider;

public class AirConditionActivity extends BaseActivity<AirConditionPresenter> implements AirConditionContract.View, AirConditionListAdapter.OnItemClickListener {
    @BindView(R.id.toolbar_left)
    ImageView ivLeft;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.air_condition_list_rv)
    RecyclerView rvList;
    private THOpenSDK thOpenSDK;
    private int currentRoomId;
    private List<RemoterInfo> airConditionList = new ArrayList<>();
    private AirConditionListAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_air_condition;
    }

    @Override
    protected void initEventAndData() {
        thOpenSDK = THOpenSDK.getInstance();
        currentRoomId = getIntent().getIntExtra("current_room_id", -1);
        initView();
        initData();
    }

    private void initData() {
        List<RemoterInfo> remoterList = thOpenSDK.getRemoterList();
        if (remoterList != null && remoterList.size() > 0) {
            airConditionList.clear();
            for (int i = 0; i < remoterList.size(); i++) {
                if (currentRoomId == remoterList.get(i).getRoomId() && remoterList.get(i).getRemoterType() == RemoterInfo.HOME_REMOTER_TYPE_AIR_CONDITION) {
                    airConditionList.add(remoterList.get(i));
                }
            }
            adapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        ivLeft.setImageResource(R.mipmap.back);
        tvTitle.setText(R.string.air_condition);
        adapter = new AirConditionListAdapter(airConditionList, mContext);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        adapter.setListener(this);
        rvList.setAdapter(adapter);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onItemClick(int position) {
        Intent intent = new Intent(mContext, AirConditionControlActivity.class);
        intent.putExtra("remoter_id", airConditionList.get(position).getRemoterId());
        intent.putExtra("remote_name", airConditionList.get(position).getRemoterName());
        startActivity(intent);
    }

    @OnClick({R.id.toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
        }
    }

}
